package com.zysoft.system.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.zysoft.common.entity.AbstractEntity;
@Entity
@Table(name = "SYS_Company")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Company extends AbstractEntity{
	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false)
    private Integer id;
	private String name;
	private String code;
	private String address;
	private boolean disenable = false;
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="cityId")
	private ArgumentsTree city;
	@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE })  
    @JoinTable(name = "SYS_Company_Department", joinColumns = { @JoinColumn(name = "companyId") }, inverseJoinColumns = { @JoinColumn(name = "departmentId") }) 
	private Set<Department> departments;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public boolean isDisenable() {
		return disenable;
	}
	public void setDisenable(boolean disenable) {
		this.disenable = disenable;
	}
	public ArgumentsTree getCity() {
		return city;
	}
	public void setCity(ArgumentsTree city) {
		this.city = city;
	}
	public synchronized Set<Department> getDepartments() {
		if(departments == null)
			departments =  new HashSet<Department>();
		return departments;
	}
	public void setDepartments(Set<Department> departments) {
		this.departments = departments;
	}
	
	public void addChild(Department department) {
		if(department != null)
			getDepartments().add(department);
	}

	public void removeChild(Department department) {
		if (department!=null && getDepartments().contains(department)) {
			getDepartments().remove(department);
		}
	}

	
}
